define(function() {
    return ['$scope', '$timeout','$location','$interval', function($scope, $timeout, $location,$interval) {
        //隐藏遮罩层
    	$scope.ldState = false;
    	$scope.ldStateNav = false;
    	$scope.ldStateTpr = false;
//    	$timeout(function(){$scope.animateEnter = true;},0);
//    	cordova.InAppBrowser.open('http://www.baidu.com', '_blank', 'location=yes');
    	
    	//点击去掉导航动画
    	$scope.displayFn = function(){
    		$scope.isfade_out = true;
    		$timeout(function(){
    			$scope.isfade_out = false;
    			document.querySelector('#gdc_page').style.display = 'none';
    		},1000);
    		
    	}
    	//监听标题栏
        $scope.$on('changeNavTitle',function(e,data){
            $scope.$broadcast('changeNavTitleFromBody',data)
        });
    	//监听收藏成功是否显示
    	$scope.showCollect = false;//默认隐藏收藏层
    	$scope.$on('showCollect', function(event, data) {
    		$timeout(function(){
    			$scope.showCollect = data;
    		},0)
        });
    	
    	//监听提示信息
    	$scope.$on('promptAlertMsg', function(event, data) {
    		$timeout(function(){
    			$scope.promptAlertMsg = data;
    		},0)
        	
        });
    	
    	$scope.$on('redPointFlag', function(event, data) {
    		//向下推送我的红点
    		$scope.$broadcast("mineRedPoint",data);
        });
    	
    	//监听子集密码软键盘是否显示
        $scope.$on('pwdSlidUp', function(event, data) {
        	$scope.pwdSlidShow = data;
        });
        $scope.password = "";
        $scope.$watch('password', function(newValue, oldValue) {
        	$scope.$broadcast("pwdChange",$scope.password);
        });
        
        //监听保存的密码
        $scope.$on('assignPwd', function(event, data) {
        	$scope.assignPwd = data;
        });
    	
        //监听子集显示顶部显示
        $scope.$on('menuTypeLight', function(event, data) {
        	$scope.menuType = data;
        });
        
        // 全黑loading
        $scope.$on('loadingChange', function(event, data) {
            $scope.ldState = data;
        });
        
        // 显示导航条loading
        $scope.$on('loadingChangeNav', function(event, data) {
            $scope.ldStateNav = data;
        });
        
        // 显示半透明背景oading
        $scope.$on('loadingChangeTpr', function(event, data) {
            $scope.ldStateTpr = data;
        });
        
        // 监听子级是否显示底部menu层
        $scope.$on('bub-menu', function(event,data) {
            $scope.isMenuShow = data;
        });
        
        // 监听子级是否显示商城顶部
        $scope.$on('top-show', function(event,data) {
            $scope.topShow = data;
        });
        
      //监听顶部显示标示 (顶部显示'1'：显示案源库)
        $scope.$on('flag-tag', function(event,data) {
            $scope.falgTag = data;
        });
        
        //监听特殊返回事件(左侧返回键)
        $scope.manageMenu = function(routeUrl){
        	 if(routeUrl == '/'){
        	 	home.toBack(null,function(){},null);
//        		 document.addEventListener("backbutton", exitApp, false);//绑定退出事件
//        		 // 3秒后重新注册 
//                 var intervalID = window.setInterval(function() { 
//                         window.clearInterval(intervalID); 
//                         document.removeEventListener("backbutton", exitApp, false); // 注销返回键 
//                 }, 3000); 
        	 }else if(routeUrl.indexOf('\/messageList') > -1||/^\/applyRisk$/.test(routeUrl)||routeUrl.indexOf('\/investDetail') > -1){
                 $scope.$broadcast('backAllButtonClick');
             }else {
//        		$scope.animateEnter = false;
        		 $scope.ldState = false;
    	    	 $scope.ldStateNav = false;
    	    	 $scope.ldStateTpr = false;
    	    	 if(routeUrl === '/mine'){
    	    		$scope.$emit('goNext','/');
    	    	 }else if(routeUrl === '/mineLs'){
    	    		 $scope.$emit('goNext','/');
    	    	 }else{
    	    		 history.back();
    	    	 }
        	 }
        }
        
      //监听特殊返回事件(右侧管理键)
        $scope.manageRightBt = function(rightManage){
        	if(rightManage.name == "registerEvent"){
        		$scope.$broadcast('registerEvent', true);
        	}else if(rightManage.name == "skipEvent"){
        		$scope.$broadcast('skipEvent', true);
        	}else if(rightManage.name == "deleteEvent"){
        		$scope.$broadcast('deleteEvent', true);
        	}else if(rightManage.name == "deleteComEvent"){
        		$scope.$broadcast('deleteEvent', false);
        	}else if(rightManage.name == "collectionEvent"){
        		$scope.$broadcast('collectionEvent', true);
        	}else if(rightManage.name == "deleteCollectEvent"){
        		$scope.$broadcast('deleteCollectEvent', true);
        	}else if(rightManage.name == "deleteCollectComEvent"){
        		$scope.$broadcast('deleteCollectEvent', false);
        	}else if(rightManage.name == "shareEvent"){
        		$scope.$broadcast('shareEvent', true);
        	}else if(rightManage.name == "editTextEvent"){
        		$scope.$broadcast('topNavEditTextEvent', true);
        	}
        }
        
        function exitApp(){ 
            navigator.app.exitApp(); 
        } 
        
        // 页面跳转之前，先修改动画方向
        $scope.$on('goNext', function(event, data) {
//        	if(!$scope.animateEnter){
//       			 $scope.animateEnter = true;
//        	}
            $location.url(data);
//            $route.reload();刷新当前页面
//          $location.path(data).replace();想改变当前 URL 而不创建新的历史记录的话
        });
        
     // 页面跳转之前，先修改动画方向
//        $scope.$on('goBack', function(event, data) {
//          $scope.animateEnter = false;
//          $location.path(data);
//        });
        
     // 首页左右滑动效果
        function scollEffect(){
			var el = document.querySelector('#gdc_page .scl_area'),
				ele = el.querySelector('#pageScroll'),
				dot = document.querySelector('.blue_dot'),
				prt_width = window.getComputedStyle(el).width.slice(0, -2);
				ele.style.width = prt_width + 'px';
			var dis = [],
				pos = 1,
				mp = 2,//设置张数
				isSwitch = 0,
				originPos,
				retraceFlag = 0;

			var changePos = function(pos) {
				var dis = (pos - 1) * 20 + 'px';
				dot.style['-webkit-transform'] = 'translate3d(' + dis + ', 0, 0)';
			};

			el.addEventListener('touchstart', function(e) {
				isSwitch = 0;
				originPos = dis[0] = dis[1] = e.targetTouches[0].pageX;
			}, false);

			var startString = 'translate3d(',
				endString = 'px, 0, 0)';

			el.addEventListener('touchmove', function(e) {
				e.preventDefault();
				if (dis[1] !== e.targetTouches[0].pageX) {
				  if ((dis[1] - dis[0]) * (e.targetTouches[0].pageX - dis[1]) < 0) {
					retraceFlag = 1;
				  }
				  dis[0] = dis[1];
				  dis[1] = e.targetTouches[0].pageX;
				  var dist = dis[1] - originPos;
				  isSwitch = (Math.abs(dist) >  40) ? 1 : isSwitch;
					ele.style['-webkit-transition-duration'] = '0s';
				  if (dist < 0) {
					  if (pos !== mp) {
						ele.style['-webkit-transform'] = startString + ((1 - pos) * prt_width + dist)/2 + endString;
					  }
				  } else if (dist > 0) {
					  if (pos !== 1) {
						ele.style['-webkit-transform'] = startString + ((1 - pos) * prt_width + dist)/2 + endString;
					  }
				  }
				}
			}, false);

			el.addEventListener('touchend', function(e) {
			  if (isSwitch && (retraceFlag === 0)) {
					if (dis[1] - dis[0] < 0) {
					  if (pos !== mp) {
						  ele.style['-webkit-transition-duration'] = '0.5s';
						  ele.style['-webkit-transform'] = startString + (-prt_width * pos)/2 + endString;
						  pos += 1;
						  changePos(pos);
					  }
					} else if (dis[1] - dis[0] > 0) {
					  if (pos !== 1) {
						  ele.style['-webkit-transition-duration'] = '0.5s';
						  ele.style['-webkit-transform'] = startString + (-prt_width * (pos - 2))/2 + endString;
						  pos -= 1;
						  changePos(pos);
					  }
					}
			  } else {
				ele.style['-webkit-transition-duration'] = '0.2s';
				ele.style['-webkit-transform'] = startString + (-prt_width * (pos - 1))/2 + endString;
				retraceFlag = 0;
			  }
			}, false);
        }
        $scope.$apply();
    }];
});